package com.botarena.client.contest;

import com.botarena.client.ui.ErrorDialogBox;
import com.botarena.client.ui.GenericDialogBox;
import com.botarena.client.ui.InformationDialogBox;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class NewContestForm extends FormPanel {

	public TextBox name = new TextBox();
	public TextBox from = new TextBox();
	public TextBox to = new TextBox();

	public NewContestForm(Label status) {
		VerticalPanel panel = new VerticalPanel();

		setAction("/admin/servlet/http/createContest");
		setEncoding(FormPanel.ENCODING_URLENCODED);
		setMethod(FormPanel.METHOD_POST);
		setWidget(panel);

		// DatePicker datePicker = new DatePicker();

		HorizontalPanel namePanel = createDatePanel("Name: ", "name", name);
		HorizontalPanel fromPanel = createDatePanel("From: ", "from", from);
		HorizontalPanel toPanel = createDatePanel("To: ", "to", to);

		Button create = createButtonCreate(status);

		Label comment = new Label("Set contest dates (dd.MM.yyyy HH:mm)");
		comment.setStyleName("title6");
		panel.add(comment);
		panel.add(namePanel);
		panel.add(fromPanel);
		panel.add(toPanel);
		panel.add(create);
		panel.setStyleName("formTable");
	}

	private HorizontalPanel createDatePanel(String labelName, String fieldName,
			TextBox text) {
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(new Label(labelName));

		text.setName(fieldName);
		panel.add(text);

		return panel;
	}

	private Button createButtonCreate(final Label status) {

		Button create = new Button("create", new ClickHandler() {
			public void onClick(ClickEvent event) {
				submit();
			}
		});

		addSubmitHandler(new FormPanel.SubmitHandler() {

			@Override
			public void onSubmit(SubmitEvent event) {

				DateTimeFormat format = DateTimeFormat
						.getFormat("dd.MM.yyyy HH:mm");

				System.out.println(name.getText());

				try {
					format.parse(from.getText());
					format.parse(to.getText());

					if (name.getText().length() == 0) {
						GenericDialogBox dialog = new InformationDialogBox(
								"Contest");
						dialog.setMessage("The name field must not be empty");
						dialog.showRelativeTo(name);
						event.cancel();
					}

				} catch (IllegalArgumentException e) {
					GenericDialogBox dialog = new InformationDialogBox(
							"Contest");
					dialog.setMessage("Date format incorrect");
					dialog.showRelativeTo(from);
					event.cancel();
				}

			}

		});

		addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
				String result = event.getResults();

				if (result.length() == 0) {
					Window.Location.reload();
				} else {
					GenericDialogBox dialog = new ErrorDialogBox(
							"Create contest");
					dialog.setMessage(result);
					dialog.center();
				}
			}
		});

		return create;
	}

}
